/**
 * 缩略图处理
 */
package jinyilw.swing.image;

import jinyilw.common.TextTools;
import jinyilw.common.app.AppConfig;
import jinyilw.common.file.FileTools;
import jinyilw.swing.ResourceManager;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.Vector;

/**
 * @author Jinyi
 */
public class ThumbMaker
{
	// public static final int defaultThumbW = 150;
	private static volatile List<ThumbMaker> thumbsList;
	private int thumbW = 150, thumbH = 110, radius = 0;
	private File imageFile, thumbFile;
	private Image image;
	private Image thumbnail;
	private boolean saved;

	public ThumbMaker()
	{
	}

	public ThumbMaker(int width, int height, int radius)
	{
		this.thumbW = width;
		this.thumbH = height;
		this.radius = radius;
	}

	public ThumbMaker(File file)
	{
		setFile(file);
	}

	public ThumbMaker(String path)
	{
		setFile(FileTools.getUserFile(path));
	}

	public static void createThumbsList()
	{
		if (thumbsList == null)
			synchronized (ThumbMaker.class)
			{
				if (thumbsList == null)
				{
					thumbsList = new Vector<>();
					AppConfig.addAppExitedOperator(o -> {
						for (ThumbMaker thumbMaker : thumbsList)
							thumbMaker.saveThumbnail();
					});
				}
			}
	}

	public void setFile(File file)
	{
		if (file == null || file == imageFile)
			return;
		imageFile = file;
		thumbFile = createThumbFile(file);
		thumbnail = null;
		image = null;

		boolean thumbExist = thumbFile.exists();
		if (thumbExist)
			thumbnail = ResourceManager.getImage(thumbFile);
		if (thumbnail == null)
			getOriginalImage();
		if (isImage() && !thumbExist && !saved)
		{
			if (thumbsList == null)
				createThumbsList();
			thumbsList.add(this);
			saved = true;
		}
	}

	public File getImageFile()
	{
		return imageFile;
	}

	public Image getOriginalImage()
	{
		if (image == null)
			image = ImageTools.toBufferedImage(ResourceManager.getImage(imageFile));
		if (image == null && thumbFile != null)
			thumbFile.delete();
		if (image != null && thumbnail == null)
			thumbnail = createThumbnail(image);
		return image;
	}

	public Image getThumbnail()
	{
		return ImageTools.toBufferedImage(thumbnail);
	}

	protected BufferedImage createThumbnail(Image image)
	{
		if (radius > 0)
			return ImageTools.clip(image, thumbW, thumbH, radius);
		return ImageTools.getShapedImage(image, thumbW, thumbH);
	}

	protected File createThumbFile(File file)
	{
		String name = TextTools.getFileNameNoExt(file.getName());
		return new File(file.getParentFile(),
				".thumb" + File.separator + name + ".png");
	}

	public static File getDefaultThumbFile(File file)
	{
		if (file == null)
			return null;
		String name = TextTools.getFileNameNoExt(file.getName());
		return new File(file.getParentFile(),
				".thumb" + File.separator + name + ".png");
	}

	public boolean isImage()
	{
		return thumbnail != null;
	}

	public void saveThumbnail()
	{
		File thumbFolder = thumbFile.getParentFile();
		if (!thumbFolder.exists())
			FileTools.createHiddenDirectory(thumbFolder);
		ImageTools.saveImageToFile(thumbnail, thumbFile, "png");
	}

	public void renameTo(String name)
	{
		if (imageFile == null)
			return;
		String ext = TextTools.getFileExt(imageFile.getPath());
		if (ext != null)
			ext = "." + ext;
		imageFile = FileTools.renameTo(imageFile, name + ext);
		thumbFile = FileTools.renameTo(thumbFile, name + ".png");
	}

	public void delete()
	{
		if (imageFile == null)
			return;
		imageFile.delete();
		thumbFile.delete();
		if (saved)
		{
			thumbsList.remove(this);
			saved = false;
		}
	}
}
